import VPlay 1.0
import QtQuick 1.1
import "entities"

SceneBase {
    id:gameScene
    // the name of the current level gets stored here
    property string activeLevelString
    // the current level gets stored here
    property variant activeLevel

    //property variant window: window

    property alias level: loader.level
    property alias player: loader.player
    property alias entityContainer: loader.entityContainer

    // make 1 grid (so 1 block and the player size) 48 logical px - a roost has this size, so 320/48= 6.6 can be displayed in one scene
    gridSize: 48//32

    // place it on bottom, because otherwise it would be unfair compared to different devices because the player would see more to the bottom playfield!
    sceneAlignmentY: "bottom"

    // for performance-testing, if the score should updated every frame with the y value of the level
    // it is set to false, because updating a static text every frame is very poor for performance, because a texture is generated every frame!
    // thus, if this flag is set to false the score is only updated every 10th frame which speeds things up
    property bool enableConstantScoreTextUpdating: false




    // allows collision detection between the entities, so if the player collides with roosts or the corn, and for removal of the out-of-scene entities
    PhysicsWorld {
        // this id gets used in Level, to test if a new roost or window can be built on the new position or if there is an entity already
        id: physicsWorld
        // this puts it on top of all other items for the physics debug renderer
        z: 1

        // for physics-based games, this should be set to 60!
        updatesPerSecondForPhysics: 60
        // make objects fall faster by increasing gravity
        gravity.y: 13

        // this should be increased so it looks good, by default it is set to 1 to save performance
        velocityIterations: 55
        positionIterations: 55

        //y: level.y // uncomment this, if you want the physics world move with the level - this is only for testing the debugDraw, and has no effect on the game logic ans the physics positions are not mapped back to the physicsWorld!
    }

    // this allows usage of the left and right keys on desktop systems or mobiles with physical keyboards
    // focus must be set to visible, not just to true, because when the scene gets invisible, it looses focus and would never get set to true again!
    // forward the input to the controller of the player
    Keys.forwardTo: player.controller

    // replaced key controls by accelerometer mapping
    Accelerometer {
      id: accelerometer

      onReadingChanged: {
        // Note: The original *10 was too responsive (too fast) so it was changed to *5
        player.controller.xAxis = reading.x * 3
        player.controller.yAxis = reading.y * 3
      }
    }

    // used to steer car by pressing images, currently not needed
//    Image {
//        source: "img/arrow-left.png"
//        opacity: 0.5
//        width: 48
//        height: 48
//        anchors {
//            left: gameScene.gameWindowAnchorItem.left
//            bottom: gameScene.gameWindowAnchorItem.bottom
//            leftMargin: 10
//            bottomMargin: 10
//        }
//    }
//    Image {
//        source: "img/arrow-left.png"
//        opacity: 0.5
//        width: 48
//        height: 48
//        mirror: true
//        anchors {
//            right: gameScene.gameWindowAnchorItem.right
//            bottom: gameScene.gameWindowAnchorItem.bottom
//            rightMargin: 10
//            bottomMargin: 10
//        }
//    }

    MouseArea {
        // use the full window as control item, press anywhere on the left half for steering left, on the right half for steering right
        anchors.fill: gameScene.gameWindowAnchorItem
        onPressed: {
            console.debug("onPressed, mouseX", mouseX)
            if(mouseX > gameScene.gameWindowAnchorItem.width/2)
                player.controller.xAxis = 1;
            else
                player.controller.xAxis = -1;
        }
        onPositionChanged: {
            if(mouseX > gameScene.gameWindowAnchorItem.width/2)
                player.controller.xAxis = 1;
            else
                player.controller.xAxis = -1;
        }
        onReleased: player.controller.xAxis = 0
    }

    Text {
        id: carHealthtxt
        x: 5
        // place it on top of the window, not on top of the logical scene
        anchors.top: gameScene.gameWindowAnchorItem.top
        anchors.topMargin: 5

        text: "Car Health: " + player.carHealth
        //font.family: fontHUD.name
        font.pixelSize: 22
        color: "yellow"
        z: 3
    }
    Text {
        id: carFuel
        x: 5
        // place it on top of the window, not on top of the logical scene
        anchors.top: carHealthtxt.bottom
        anchors.topMargin: 5

        text: "Greek Fuel: " + player.fuel
        //font.family: fontHUD.name
        font.pixelSize: 22
        color: "yellow"
        z: 3
    }
    Text {
        x: 5
        // place it on top of the window, not on top of the logical scene
        anchors.top: carFuel.bottom
        anchors.topMargin: 5

        text: "Rockets: " + player.rockets
        //font.family: fontHUD.name
        font.pixelSize: 22
        color: "yellow"
        z: 3
    }

    // gets called by ChickenOutbreakMain when this scene gets active
    // starts a game again - stopGame() was called before so it is save to call that here
    function enterScene() {
        level.startGame();
    }


    // back button to leave scene
    SimpleButton {
        text: "Back to menu"
        anchors.right: parent.right
        z: 1
        onClicked: backPressed()
    }

    // name of the current level
    Text {
        color: "blue";
        text: activeLevel === undefined? "" : gameScene.activeLevel.levelName;
    }

    // load levels at runtime
    Loader {
        id: loader

        property variant level: item ? item : {}
        property variant player: item ? item.player : {}
        property variant entityContainer: item ? item : {}

        source: "levels/" + gameScene.activeLevelString + ".qml"
        onLoaded: {
            // do not forget to call loadItemWithCocos or the item might not be displayed!
            loadItemWithCocos(item)
            activeLevel = item
            item.width = gameScene.width
            item.height = gameScene.height
            entityContainer= loader.entityContainer

            //enterScene();
        }

    }

    /* // background image (-hd and -hd2 will be automatically used for higher resolutions)
        MultiResolutionImage {
            source: "img/gamebg-sd.png"
            anchors.centerIn: parent
        }*/

    // set the name of the current level, this will cause the Loader to load the corresponding level
    function setLevel(level) {
        activeLevelString = level
    }



}
